package com.expert.miniapp.service.impl;

import com.expert.miniapp.dao.InstrumentMapper;
import com.expert.miniapp.dao.Instrument;
import com.expert.miniapp.service.InstrumentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * 仪器设备业务逻辑实现类
 */
@Service
public class InstrumentServiceImpl implements InstrumentService {

    @Autowired
    private InstrumentMapper instrumentMapper;

    @Override
    public Instrument getInstrumentById(String instrumentId) {
        if (instrumentId == null || instrumentId.trim().isEmpty()) {
            return null;
        }
        return instrumentMapper.findById(instrumentId);
    }

    @Override
    public List<Instrument> getInstrumentsByCategory(String category) {
        if (category == null || category.trim().isEmpty()) {
            return instrumentMapper.findAll();
        }
        return instrumentMapper.findByCategory(category);
    }

    @Override
    public PageInfo<Instrument> getAllInstruments(Integer pageNum, Integer pageSize) {
        pageNum = (pageNum == null || pageNum < 1) ? 1 : pageNum;
        pageSize = (pageSize == null || pageSize < 1) ? 10 : pageSize;
        PageHelper.startPage(pageNum, pageSize);
        List<Instrument> instruments = instrumentMapper.findAll();
        return new PageInfo<>(instruments);
    }

    @Override
    public PageInfo<Instrument> getRecommendedInstruments(Integer pageNum, Integer pageSize) {
        pageNum = (pageNum == null || pageNum < 1) ? 1 : pageNum;
        pageSize = (pageSize == null || pageSize < 1) ? 10 : pageSize;
        PageHelper.startPage(pageNum, pageSize);
        List<Instrument> instruments = instrumentMapper.findByWeightOrder();
        return new PageInfo<>(instruments);
    }

    @Override
    public PageInfo<Instrument> searchInstruments(String keyword, Integer pageNum, Integer pageSize) {
        pageNum = (pageNum == null || pageNum < 1) ? 1 : pageNum;
        pageSize = (pageSize == null || pageSize < 1) ? 10 : pageSize;
        PageHelper.startPage(pageNum, pageSize);
        List<Instrument> instruments = instrumentMapper.searchByKeyword(keyword);
        return new PageInfo<>(instruments);
    }
}